package cn.com.thingo.hyintelligent.widget;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.util.ToastUtil;

@SuppressLint("ValidFragment")
public class DeleteAuthDialogFragment extends DialogFragment {
    private Context context;
    private CallBack callBack;
    private int position;
    @BindView(R.id.dialog_del_auth_determine)
    TextView tvDetermine;
    @BindView(R.id.dialog_del_auth_cancel)
    TextView tvCancel;

    public DeleteAuthDialogFragment(Context context, int position) {
        this.context = context;
        this.position = position;
        callBack = (CallBack) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_del_auth, null);
        ButterKnife.bind(this, view);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(view);
        return builder.create();
    }

    @OnClick({R.id.dialog_del_auth_determine, R.id.dialog_del_auth_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_del_auth_determine:
                callBack.determine(position);
                this.dismiss();
                break;
            case R.id.dialog_del_auth_cancel:
                this.dismiss();
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            Window window = dialog.getWindow();
            if (window != null) {
                window.setLayout((int) (dm.widthPixels * 0.75), ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        }
    }

    public interface CallBack {
        void determine(int position);
    }
}
